package com.endbd.systemmanagement.controller.job;

import com.endbd.feignapi.model.system.Job;
import com.endbd.systemmanagement.model.GrantModel;
import com.endbd.systemmanagement.model.JobCondition;
import com.endbd.systemmanagement.model.ModuleImpl;
import com.endbd.systemmanagement.model.ResultBody;
import com.endbd.systemmanagement.service.job.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/system")
@RestController
public class JobController {

  @Autowired
  private JobService jobService;

  @GetMapping("/job/joblist")
  public ResultBody getJobList(){
    ResultBody resultBody = new ResultBody();
    List<Job> jobList = jobService.getJobList();
    if(jobList.isEmpty()) {
      resultBody.setCode(500);
      resultBody.setMessage("数据获取失败");
    }else{
      resultBody.setData(jobList);
      resultBody.setCode(200);
      resultBody.setMessage("success");
    }
    return resultBody;
  }

  @PostMapping("/job/queryCondition")
  public ResultBody queryCondition(@RequestBody JobCondition jobCondition){
    List<Job> list=jobService.queryCondition(jobCondition);
    ResultBody resultBody = new ResultBody();
    if(list.isEmpty()){
      resultBody.setCode(500);
      resultBody.setMessage("没有相应数据");
    }else{
      resultBody.setCode(200);
      resultBody.setMessage("success");
      resultBody.setData(list);
    }
    return resultBody;
  }

  @PostMapping("/job/insert")
  public ResultBody createJob(@RequestBody Job job){
    ResultBody resultBody = new ResultBody();
    boolean b=jobService.createJob(job);
    if(b){
      resultBody.setCode(200);
      resultBody.setMessage("新建职位=>"+job.getJobName());
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("error");
    }
    return resultBody;
  }

  @GetMapping("/job/zx/{id}/{state}")
  public ResultBody zx(@PathVariable Integer id, @PathVariable String state){
    ResultBody resultBody = new ResultBody();
    int b=jobService.zx(id,state);
    if(b==1){
      resultBody.setCode(200);
      resultBody.setMessage("更改职位状态=>"+id+"=>"+(state.equals("1") ? "注销" : "开启"));
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("error");
    }
    return resultBody;
  }

  @PostMapping("/job/update")
  public ResultBody updateJob(@RequestBody Job job){
    ResultBody resultBody = new ResultBody();
    boolean b=jobService.updateJob(job);
    if(b){
      resultBody.setCode(200);
      resultBody.setMessage("更改职位信息=>"+job.getJobName());
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("error");
    }
    return resultBody;
  }

  @GetMapping("/job/getGrant/{id}")
  public ResultBody getGrant(@PathVariable Integer id){
    ResultBody resultBody = new ResultBody();
    List<Integer> list=jobService.getGrant(id);
    if(list==null){
      resultBody.setCode(500);
      resultBody.setMessage("获取权限失败");
    }else{
      resultBody.setCode(200);
      resultBody.setMessage("success");
      resultBody.setData(list);
    }
    return resultBody;
  }

  @GetMapping("/job/modules")
  public ResultBody getModules(){
    ResultBody resultBody = new ResultBody();
    List<ModuleImpl> list=jobService.getModules();
    if(list.isEmpty()){
      resultBody.setCode(500);
      resultBody.setMessage("模块数据获取失败");
    }else{
      resultBody.setCode(200);
      resultBody.setMessage("success");
      resultBody.setData(list);
    }
    return resultBody;
  }

  @PostMapping("/job/endowGrant/{id}")
  public ResultBody endowGrant(@RequestBody GrantModel grantModel, @PathVariable Integer id){
    ResultBody resultBody = new ResultBody();
    boolean b= false;
    try {
      b = jobService.endowGrant(id,grantModel.getInitKey(),grantModel.getChecked());
    } catch (Exception e) {
      e.printStackTrace();
    }
    if(b){
      resultBody.setCode(200);
      resultBody.setMessage("更新了职位"+id+"的权限");
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("error");
    }
    return resultBody;
  }

}
